package com.example.demo.encode;

public class AesKeyExpand {

    /*
     * Number of 32-bit words comprising the Cipher Key. For this
     * standard, Nk = 4, 6, or 8.
     */
    private int Nk;
    /*
     * Number of rounds, which is a function of  Nk  and  Nb (which is
     * fixed). For this standard, Nr = 10, 12, or 14.
     */
    // Nr 为给定密钥大小的轮数减 1。加密算法使用的轮数要么是10，12，要
    // 么是14，这依赖于种子密钥长度是128位、192位还是256位
    private int Nr;
    /*
     * Generates the round constant Rcon[i] //轮常数表
     */
	/* 每个轮常数的最左边的字节是GF(28)域中2的幂次方。它的另一个表示方法是其每个
		值是前一个值乘上0x02。注意 0x80 × 0x02 = 0x1b 是 0x80 左移1个比特位
		后紧接着与 0x1b 进行异或。
	*/
    private byte Rcon[] = {0x6d, 0x73, 0x2f, (byte) 0xca};
    /* 默认key值  */
    private byte keyValue[];
    /* key拓展值  */
    private byte keyExpand[];
    /* 数据应当大于等于16k */
    private byte saltData[];

    public AesKeyExpand(byte[] keyValue, byte[] saltData) {
        this.setKeyValue(keyValue);
        this.setSaltData(saltData);
    }


    public int getNk() {
        return Nk;
    }

    public void setNk(int nk) {
        Nk = nk;
    }

    public int getNr() {
        return Nr;
    }

    public void setNr(int nr) {
        Nr = nr;
    }

    public byte[] getRcon() {
        return Rcon;
    }

    public void setRcon(byte[] Rcon) {
        this.Rcon = Rcon;
    }

    public byte[] getKeyValue() {
        return keyValue;
    }

    public void setKeyValue(byte[] keyValue) {
        int uiLoop, i, j;
        int uikeyLen = keyValue.length;
        if (uikeyLen >= 32) {
            uikeyLen = 32;
        } else if (uikeyLen >= 24) {
            uikeyLen = 24;
        } else if (uikeyLen >= 16) {
            uikeyLen = 16;
        } else {
            System.out.println(" key的值太短，拓展到16字节，不足的字节用前面的字节补充! ");
        }

        if (uikeyLen >= 16) {
            this.keyValue = new byte[uikeyLen];
            for (uiLoop = 0; uiLoop < uikeyLen; uiLoop++) {
                this.keyValue[uiLoop] = keyValue[uiLoop];
            }
        } else {
            System.out.println("The value length should be longger than or equal with 16.");
            this.keyValue = new byte[16];
            for (i = 0; i < keyValue.length && i < 16; i++) {
                this.keyValue[i] = keyValue[i];
            }
            for (j = 0; i < 16; i++) {
                this.keyValue[i] = this.keyValue[j];
            }
        }
    }

    public byte[] getKeyExpand() {
        return keyExpand;
    }

    public void setKeyExpand(byte[] keyExpand) {
        this.keyExpand = keyExpand;
    }

    public byte[] getSaltData() {
        return saltData;
    }

    public void setSaltData(byte[] saltData) {
        int i, j;
        if (16 > saltData.length) {
            System.out.println("The value length should be longger than or equal with 16.");
            this.saltData = new byte[16];
            for (i = 0; i < saltData.length; i++) {
                this.saltData[i] = saltData[i];
            }
            for (j = 0; i < 16; i++) {
                this.saltData[i] = saltData[j];
            }
        } else {
            this.saltData = saltData;
        }
    }

}
